<?php

namespace App\Http\Controllers\Admin;
use App\Partner;
use Illuminate\Support\Facades\DB;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Input;
use App\Manage;
use App\Role;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Request;

class ManageController extends Controller
{
    //定义一个数组  用来排列查询选项或列表头
    protected static $field = [
        ['id'=>1,'searchtype'=>'编号'],
        ['id'=>2,'searchtype'=>'创建时间'],
        ['id'=>3,'searchtype'=>'更新时间'],
        ['id'=>4,'searchtype'=>'管理员名称'],
        ['id'=>5,'searchtype'=>'角色名称'],
        ['id'=>6,'searchtype'=>'邮箱'],
        ['id'=>7,'searchtype'=>'电话'],
        ['id'=>8,'searchtype'=>'上次登录ip'],
        ['id'=>9,'searchtype'=>'上次登录时间'],
        ['id'=>10,'searchtype'=>'登陆次数'],
        ['id'=>11,'searchtype'=>'状态'],
    ];
    //载入管理员列表页
    public function index()
    {
        $search = Input::get('searchtype');
        $param = Input::get('param');
        $obj = new Manage;
        if($search == 1){
            $obj->where('id','like',"%$param%");
            //dd($obj);
        }elseif ($search == 2){
            $obj->where('create_time','like',"%$param%");
        }elseif ($search == 3){
            $obj->where('update_time','like',"%$param%");
        }elseif ($search == 4){
            $obj->where('username','like',"%$param%");
        }elseif ($search == 5){
            $id = Role::where('name','like',"%$param%")->get()->toArray();
            if(!$id){
                return  redirect()->back()->withInput()->withErrors('未搜索到该角色！');
            }
            $obj->where('roleid','=',$id[0]['id']);
        }elseif ($search == 6){
            $obj->where('email','like',"%$param%");
        }elseif ($search == 7){
            $obj->where('phone','like',"%$param%");
        }elseif ($search == 8){
            $obj->where('ip','like',"%$param%");
        }elseif ($search == 9){
            $obj->where('login_time','like',"%$param%");
        }elseif ($search == 10){
            $obj->where('login_number','like',"%$param%");
        }elseif ($search == 11){
            $obj->where('status','like',"%$param%");
        }
        $manages = $obj->where('roleid',"!=",4)->where('roleid',"!=",1)->Paginate(10)->toArray();
        if($manages == false && !empty($search)){
            return  redirect()->back()->withInput()->withErrors('未查询到结果！');
        }
        for($i=0; $i<count($manages['data']);$i++){
           if($manages['data'][$i]['status'] ==1) {
               $manages['data'][$i]['status'] = "1(已启用)";
           }else{
               $manages['data'][$i]['status'] = "0(已禁用)";
           }
           $manages['data'][$i]['ip'] = long2ip($manages['data'][$i]['ip']);
            $role = DB::table('role')->where('id',$manages['data'][$i]['roleid'])->first();
            $manages['data'][$i]['rolename'] = $role->name;
        }
        $manages['searchtype'] = self::$field;
        $role = Role::all();
        $man = $obj->where('roleid',"!=",4)->where('roleid',"!=",1)->Paginate(10);
        return view('admin.manage.manageList',['manages'=>$manages,'man'=>$man]);
    }

    //跳转到添加管理员页
    public function add()
    {
        $list = Role::all()->toArray() ;
//        $man = Manage::where('roleid',4)->get();
        return view('admin.manage.addManage',['list'=>$list]);
        //
    }

    //跳转到添加兑换专员页
    public function addChange()
    {
        $list = Manage::where('roleid',2)->get();
        return view('admin.manage.addChange',['list'=>$list]);
    }

    //账号禁用/启用
    public function setStatus($id,$status)
    {
        $role = Session::get('roleid');
        if(!empty($role) && $role != 1){
            return  back()->withErrors('没有权限！');
        }
        if($id == 1){
            return back()->withErrors('不能启/禁用自己!');
        }
        $c = Manage::where('id',$id)->where('status',$status)->get();
//        dd($c);
        if(empty($c)){
            return  back()->withErrors('已经设置！');
        }
        $map = Manage::where('id',$id)->update(['status'=>$status]);
        if($map){
            return  back()->withErrors('设置成功！');
        }else{
            return  back()->withErrors('设置失败！');
        }
    }

    //添加业务管理员
    public function addManage()
    {
        //dd(Input::all());
        $username = Input::get('name');
        $password = Input::get('password');
        $email = input::get('email');
        $phone = input::get('phone');
        $role = Session::get('roleid');
//        $man = Input::get('man');
        //dd($man);
        if (!empty($role) && $role != 1){
            return  redirect()->back()->withInput()->withErrors('没有权限！');
        }
        $list = Manage::insert(['username'=>$username , 'password'=>md5($password) ,'login_time'=>date("Y-m-d H:i:s"), 'roleid'=>2,'email'=>$email,'phone'=>$phone]);
        if($list){
            return Redirect::to("admin/manage")->withErrors('添加成功！');
        }else{
            return  back()->withErrors('添加失败！');
        }
    }

    //添加兑换专员
    public function change()
    {
//        dd(Input::all());
        $username = Input::get('name');
        $password = Input::get('password');
        $email = input::get('email');
        $phone = input::get('phone');
        $role = Session::get('roleid');
        $man = Input::get('man');
        //dd($man);
        if (!empty($role) && $role != 1){
            return  redirect()->back()->withInput()->withErrors('没有权限！');
        }
        $list = Manage::insert(['username'=>$username , 'password'=>md5($password) ,'login_time'=>date("Y-m-d H:i:s"), 'roleid'=>3,'son_id'=>$man,'email'=>$email,'phone'=>$phone]);
        if($list){
            return Redirect::to("admin/manage")->withErrors('添加成功！');
        }else{
            return  back()->withErrors('添加失败！');
        }
    }

    //删除管理员
    public function delManage($id)
    {
        $role = Session::get('roleid');
        if(!empty($role) && $role != 1){
            return  redirect()->back()->withInput()->withErrors('没有权限！');
        }
        $r = Manage::where('id',$id)->first()->toArray();
        if($r['roleid'] == 1){
            return  redirect()->back()->withInput()->withErrors('总管理员不能删除！');
        }
        $list = Manage::where('id',$id)->delete();
        if($list){
            return Redirect::to("admin/manage")->withErrors('删除成功！');
        }else{
            return  redirect()->back()->withInput()->withErrors('删除失败！');
        }
    }
    //获取对应id管理员信息
    public function getManage($id)
    {
        $list = Manage::where('id',$id)->first();
        $data = Role::get();
        $man = Manage::where('roleid',2)->get();
       // dd($data);
        if($list && $data){
            return view('admin.manage.updateManage',['list'=>$list,'data'=>$data,'man'=>$man]);
        }else{
            return  redirect()->back()->withInput()->withErrors('获取失败！');
        }
    }
    //修改管理员信息
    public function updateManage()
    {
        $role = Session::get('roleid');
        if (!empty($role) && $role > 2){
            return  redirect()->back()->withInput()->withErrors('没有权限！');
        }
        $id = Input::get('id');
        $role = input::get('man');
        $data['username'] = Input::get('name');
        if(empty($id) || empty($data['username'])){
            return  redirect()->back()->withInput()->withErrors('请填写完整！');
        }
        if(!empty($role)) {
            $data['son_id'] = $role;
        }
        if(!empty(Input::get('password'))){
            $data['password'] = md5(Input::get('password'));
            $data['email'] = input::get('email');
            $data['phone'] = input::get('phone');
        }
        $list = Manage::where('id',$id)->update($data);
        if($list){
            return Redirect::to("admin/manage")->withErrors('修改成功！');
        }else{
            return  back()->withErrors('修改失败！');
        }
    }

    //载入添加客户经理页面
    public function director() {
        $par = Partner::all();
        $man = Manage::where('roleid','2')->whereNull('son_id')->get()->toArray();
        return view('admin.manage.director',['par'=>$par,'man'=>$man]);
    }

    //添加信息
    public function addDire() {
        $username = Input::get('name');
        $password = Input::get('password');
        $email = input::get('email');
        $phone = input::get('phone');
        $role = Session::get('roleid');
        $partner = Input::get('partner');
        $man = Input::get('man');
//        dump($partner);
//        dump($man);
        if (!empty($role) && $role > 2){
            return  redirect()->back()->withInput()->withErrors('没有权限！');
        }
        $list = Manage::insert([
            'username'=>$username ,
            'password'=>md5($password) ,
            'login_time'=>date("Y-m-d H:i:s"),
            'roleid'=>4,
            'email'=>$email,
            'phone'=>$phone,
            'par_id'=>$partner,
            'son_id'=>$man
        ]);
        if($list){
            return Redirect::to("admin/director")->withErrors('添加成功！');
        }else{
            return  back()->withErrors('添加失败！');
        }
    }

    //客户经理列表
    public function dlist() {
        $id = Session::get('id');
        if($id == 1) {
            $list = Manage::where('roleid',4)->get();
        }
        else {
            $list = Manage::where('roleid',4)->where('son_id',$id)->get()->toArray();
        }
        return view('admin.manage.dlist',['list'=>$list]);
    }

    //获取经理信息
    public function getMan($id) {
        $list = Manage::where('id',$id)->first();
        $p = Partner::where('id',$list['par_id'])->first();
        $s = Manage::where('id',$list['son_id'])->first();
        $list['pid'] = $p['id'];
        $list['pname'] = $p['name'];
        $list['sid'] = $s['id'];
        $list['sname'] = $s['username'];
        $par = Partner::all();
//        dump($par);
        $man = Manage::where('roleid','2')->get();
        return view('admin.manage.updateDirector',['list'=>$list,'par'=>$par,'man'=>$man]);
    }

    //更新经理信息
    public function updateDire() {
        $id = Input::get('id');
        $data['username'] = Input::get('name');
        $pass = Input::get('password');
        if(!empty($pass)) {
            $data['password'] = $pass;
        }
        $data['email'] = input::get('email');
        $data['phone'] = input::get('phone');
        $data['par_id'] = Input::get('partner');
        $data['son_id'] = Input::get('man');
        $update = Manage::where('id',$id)->update($data);
        if($update) {
            return Redirect::to('admin/dlist')->withErrors('更新成功!');
        }
        else {
            return back()->withErrors('更新失败!');
        }
    }

    //删除
    public function delDirector($id) {
//        dd($id);
        if(Session::get('rolied') > 1){
            return back()->withErrors('权限不足!');
        }
        $del = Manage::where('id',$id)->delete();
        if($del) {
            return back()->withErrors('删除成功!');
        }
        else {
            return back()->withErrors('删除失败!');
        }
    }

    //管理员信息
    public function admin($id) {
        $list = Manage::where('id',$id)->first();
        return view('admin/manage/admin',['list'=>$list]);
    }

    //更改信息
    public function edit() {
        $data['username'] = input::get('username');
        $data['phone'] = input::get('phone');
        $data['email'] = input::get('email');
        $pass = input::get('pass');
        if(!empty($pass)) {
            $data['password'] = md5($pass);
        }
        $id = input::get('id');
        $map = Manage::where('id',$id)->update($data);
        if($map) {
            return Redirect::to('admin/home1')->withErrors('修改成功!');
        }
        else {
            return back()->withErrors('修改失败!');
        }
    }
}